﻿namespace WG3000_COMM.Core
{
    using System;
    using System.Collections;
    using System.Net;
    using System.Net.NetworkInformation;
    using System.Threading;

    public class wgMjController : IDisposable
    {
        public static long CommTimeoutMsMin = 300L;
        private string m_IP = "";
        private int m_PORT = 0xea60;
        private wgUdpComm wgudp;

        private long byteToLong(byte[] buff, int start, int len)
        {
            long num = 0L;
            for (int i = 0; (i < len) && (i < 4); i++)
            {
                long num3 = buff[i + start];
                num += num3 << (8 * i);
            }
            return num;
        }

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing && (this.wgudp != null))
            {
                this.wgudp.Close();
                this.wgudp.Dispose();
            }
        }

        public void SearchControlers(ref ArrayList arrController)
        {
            NetworkInterface[] allNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            ArrayList list = new ArrayList();
            ArrayList list2 = new ArrayList();
            foreach (NetworkInterface interface2 in allNetworkInterfaces)
            {
                UnicastIPAddressInformationCollection unicastAddresses = interface2.GetIPProperties().UnicastAddresses;
                if (unicastAddresses.Count > 0)
                {
                    foreach (UnicastIPAddressInformation information in unicastAddresses)
                    {
                        if (!information.Address.IsIPv6LinkLocal && (information.Address.ToString() != "127.0.0.1"))
                        {
                            this.wgudp = new wgUdpComm(information.Address);
                            Thread.Sleep(300);
                            byte[] recv = null;
                            byte[] cmd = new byte[0x40];
                            for (int i = 0; i < 0x40; i++)
                            {
                                cmd[i] = 0;
                            }
                            cmd[0] = 0x17;
                            cmd[1] = 0x94;
                            int num = this.wgudp.udp_get(cmd, (int) CommTimeoutMsMin, uint.MaxValue, null, 0xea60, ref recv);
                            if (recv != null)
                            {
                                long num4 = DateTime.Now.Ticks + 0x3d0900L;
                                if (num > 0)
                                {
                                    long num5 = 0L;
                                    num5 = this.byteToLong(recv, 4, 4);
                                    string str = $"{recv[8]}.{recv[9]}.{recv[10]}.{recv[11]}";
                                    string str2 = $"{recv[12]}.{recv[13]}.{recv[14]}.{recv[15]}";
                                    string str3 = $"{recv[0x10]}.{recv[0x11]}.{recv[0x12]}.{recv[0x13]}";
                                    string str4 = $"{recv[20]:X2}-{recv[0x15]:X2}-{recv[0x16]:X2}-{recv[0x17]:X2}-{recv[0x18]:X2}-{recv[0x19]:X2}";
                                    string str5 = information.Address.ToString();
                                    if (list.IndexOf(num5) < 0)
                                    {
                                        list.Add(num5);
                                        list2.Add($"{num5},{str},{str2},{str3},{str4},{str5}");
                                    }
                                }
                                while (DateTime.Now.Ticks < num4)
                                {
                                    if (this.wgudp.PacketCount > 0)
                                    {
                                        while (this.wgudp.PacketCount > 0)
                                        {
                                            recv = this.wgudp.GetPacket();
                                            long num6 = 0L;
                                            num6 = this.byteToLong(recv, 4, 4);
                                            string str6 = $"{recv[8]}.{recv[9]}.{recv[10]}.{recv[11]}";
                                            string str7 = $"{recv[12]}.{recv[13]}.{recv[14]}.{recv[15]}";
                                            string str8 = $"{recv[0x10]}.{recv[0x11]}.{recv[0x12]}.{recv[0x13]}";
                                            string str9 = $"{recv[20]:X2}-{recv[0x15]:X2}-{recv[0x16]:X2}-{recv[0x17]:X2}-{recv[0x18]:X2}-{recv[0x19]:X2}";
                                            string str10 = information.Address.ToString();
                                            if (list.IndexOf(num6) < 0)
                                            {
                                                list.Add(num6);
                                                list2.Add($"{num6},{str6},{str7},{str8},{str9},{str10}");
                                            }
                                        }
                                        num4 = DateTime.Now.Ticks + 0x3d0900L;
                                    }
                                    else
                                    {
                                        Thread.Sleep(100);
                                    }
                                }
                            }
                        }
                    }
                    Console.WriteLine();
                }
            }
            arrController = list2;
        }

        public int ShortPacketSend(byte[] cmd, ref byte[] recv)
        {
            int num = -13;
            try
            {
                recv = null;
                if (this.wgudp == null)
                {
                    this.wgudp = new wgUdpComm();
                    Thread.Sleep(300);
                }
                byte[] buffer = cmd;
                num = this.wgudp.udp_get(buffer, (int) CommTimeoutMsMin, uint.MaxValue, this.m_IP, this.m_PORT, ref recv);
                if (recv != null)
                {
                    return num;
                }
                num = -13;
                return -1;
            }
            catch (Exception)
            {
            }
            return num;
        }

        public int ShortPacketSend(byte[] cmd, ref byte[] recv, string pcIPAddress)
        {
            int num = -13;
            try
            {
                if (string.IsNullOrEmpty(pcIPAddress))
                {
                    return this.ShortPacketSend(cmd, ref recv);
                }
                recv = null;
                if (this.wgudp == null)
                {
                    this.wgudp = new wgUdpComm(IPAddress.Parse(pcIPAddress));
                    Thread.Sleep(300);
                }
                else if (!this.wgudp.localIP.Equals(IPAddress.Parse(pcIPAddress)))
                {
                    this.wgudp.Close();
                    this.wgudp = new wgUdpComm(IPAddress.Parse(pcIPAddress));
                    Thread.Sleep(300);
                }
                byte[] buffer = cmd;
                num = this.wgudp.udp_get(buffer, (int) CommTimeoutMsMin, uint.MaxValue, this.m_IP, this.m_PORT, ref recv);
                if (recv != null)
                {
                    return num;
                }
                num = -13;
                return -1;
            }
            catch (Exception)
            {
            }
            return num;
        }

        public string IP
        {
            get => 
                this.m_IP;
            set
            {
                this.m_IP = value;
            }
        }

        public int PORT
        {
            get => 
                this.m_PORT;
            set
            {
                if ((value >= 0) && (value < 0xffff))
                {
                    this.m_PORT = value;
                }
            }
        }
    }
}

